import Vue from 'vue'
import Vuex from 'vuex'
import { LoginAPI } from '../api/login'
Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: localStorage.getItem('token') || '',
    refreshToken: localStorage.getItem('refresh_token') || ''
  },
  mutations: {
    settoken (state, tokenStr) {
      state.token = tokenStr
      localStorage.setItem('token', tokenStr)
    },
    setrefresh_token (state, refreshTokenStr) {
      state.refreshToken = refreshTokenStr
      localStorage.setItem('refresh_token', refreshTokenStr)
    }
  },
  actions: {
    async asyncLoginActions (store, formLogin) {
      const res = await LoginAPI(formLogin)
      const { token, refresh_token: refreshToken } = res.data.data
      store.commit('settoken', token)
      store.commit('setrefresh_token', refreshToken)
      console.log(res)
    }
  },
  modules: {
  }
})
